/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edu.kamai.media.audio;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.TargetDataLine;

/**
 *
 * @author xyzhu
 */
public class wavRecord {
    
    
    public wavRecord(String fn){
        this.fileName = fn;
    }
    
    public void save() {
        AudioFormat audioFormat = getAudioFormat();
        byte audioData[] = byteArrayOutputStream.toByteArray();
        InputStream byteArrayInputStream = new ByteArrayInputStream(audioData);
        audioInputStream = new AudioInputStream(byteArrayInputStream,
        audioFormat, audioData.length / audioFormat.getFrameSize());
        // 写入文件
        try {
            File file = new File(fileName);
            AudioSystem
                .write(audioInputStream, AudioFileFormat.Type.WAVE, file);
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    private AudioFormat getAudioFormat(){
        float sampleRate = 16000.0F;// 8000,11025,16000,22050,44100
        int sampleSizeInBits = 16;// 8,16
        int channels = 1;// 1,2
        boolean signed = true;// true,false
        boolean bigEndian = false;// true,false
        return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed,
                            bigEndian);
    }
    
    public void capture(){
        try{
            audioFormat = getAudioFormat();
            DataLine.Info dataLineInfo = new DataLine.Info(
                    TargetDataLine.class, audioFormat);
            targetDataLine = (TargetDataLine) AudioSystem.getLine(dataLineInfo);
            targetDataLine.open(audioFormat);
            targetDataLine.start();
            // 创建独立线程进行录音
            Thread captureThread = new Thread(new CaptureThread());
            captureThread.start();
            
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
    
    public void stop(){
        targetDataLine.close();
        stopCapture = true;
    }
    
    class CaptureThread extends Thread {
        // 临时数组
        byte tempBuffer[] = new byte[10000];
        public void run() {
            byteArrayOutputStream = new ByteArrayOutputStream();
            totaldatasize = 0;
            stopCapture = false;
            try {
                while (!stopCapture) {
                    // 读取10000个数据
                    int cnt = targetDataLine.read(tempBuffer, 0,
                    tempBuffer.length);
                    if (cnt > 0) {
                        byteArrayOutputStream.write(tempBuffer, 0, cnt);
                        totaldatasize += cnt;
                    }
                }
                
            }catch(Exception ex){
                ex.printStackTrace();
            }
        }
    }
    
    private String fileName;
    private boolean stopCapture = false; // 控制录音标志
    private AudioFormat audioFormat; // 录音格式
    // 读取数据：从TargetDataLine写入ByteArrayOutputStream录音
    private ByteArrayOutputStream byteArrayOutputStream;
    private int totaldatasize = 0;
    private TargetDataLine targetDataLine;
    // 播放数据：从AudioInputStream写入SourceDataLine播放
    private AudioInputStream audioInputStream;
    
    public static void main(String[] args){
        System.out.println("start");
        wavRecord wr = new wavRecord("abc0.wav");
        wr.capture();
        try{
            Thread.sleep(1000);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println("stop");
        wr.stop();
        wr.save();
        
        try{
            Thread.sleep(500);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        System.out.println("start");
        wr = new wavRecord("abc1.wav");
        wr.capture();
        try{
            Thread.sleep(1000);
        }catch(Exception ex){
            ex.printStackTrace();
        }
        
        wr.stop();
        wr.save();
    }
}
